﻿/*****************************************************************************
// *
// * Copyright (c) Microsoft Corporation. All rights reserved.
// * 
// * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
// * 
// **************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace Tcvt.Packages
{
    /// <summary>
    /// Base class for all add-in commands.
    /// </summary>
    internal abstract class AddInCommand
    {
        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            m_applicationObject = (DTE2)application;
            m_addIn = (AddIn)addInInst;
            
            if (connectMode == ext_ConnectMode.ext_cm_UISetup)
            {
                Commands2 commands = (Commands2)m_applicationObject.Commands;
                AddCommand(m_addIn, commands);
                UISetup(application, connectMode, addInInst, ref custom);
            }
            
            Connect(application, connectMode, addInInst, ref custom);
        }

        protected virtual void UISetup(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref Array custom) { }

        protected virtual void Connect(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref Array custom) { }
   
        protected virtual void AddCommand(AddIn addInInstance, Commands2 commands)
        {
            object[] contextGUIDS = new object[] { };
            m_dteCommand = commands.AddNamedCommand2(addInInstance,
                                                     Name,
                                                     ButtonText,
                                                     ToolTip,
                                                     UseMSOIcon,
                                                     IconIndex == -1 ? (Object)null : (Object)IconIndex,
                                                     ref contextGUIDS,
                                                     (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                                     (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
        }

        public abstract void Exec(vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled);
        public abstract void QueryStatus(EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText);

        public abstract String ButtonText { get; }
        public abstract String Name { get; }
        public abstract String ToolTip { get; }
        public abstract bool UseMSOIcon { get; }
        public abstract int IconIndex { get; }

        protected DTE2 m_applicationObject;
        protected AddIn m_addIn;
        protected Command m_dteCommand;
    }

}
